<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    Config,Request,Cache,App
};
class Daifa extends Base
{
    //列表
    public function lists(){
      if(is_post()){
          $d = $this->post;
          $map = [
              ['and','shenhe_status','=',2],
              ['and','daifa_type','=',1],
          ];
          if($d['status']!='') $map[] = ['and','status','=',$d['status']];
          if($d['keyword']) $map[] = ['and',"user_name|order_no","like","%{$d['keyword']}%"];
          if($d['min_time']) $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
          if($d['max_time']) $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
          $w  = where($map);
          $sql = "select * from withdraw  $w[0] order by status asc,id desc".limit($d['page'],$d['limit']);
          $rs = $this->db->query($sql, $w[1],'',true);
          if($rs){
              foreach ($rs[0] as $k => $v) {
                  $rs[0][$k]["daifa_type_name"] = c("type.daifa_type.{$v['daifa_type']}");
                  $rs[0][$k]["status_name"] = c("type.withdraw_status.{$v['status']}");
                  $rs[0][$k]["shenhe_status_name"] = c("type.shenhe_status.{$v['shenhe_status']}");
                  $rs[0][$k]["daifa_status_name"] = c("type.daifa_status.{$v['daifa_status']}");
                  $rs[0][$k]["create_time"] = com_time_format($v['create_time']);
              }
              return suc($rs[0], $rs[1]);
          }else{
              suc([],0);
          }
      }
      $this->fetch();
    }


    public function detail(){
        $id = $this->get["id"];
        $rs = $this->db->find('withdraw',$id);
        return $this->fetch(['info'=>$rs]);
    }

    public function shenhe(){
        if(is_post()){
            $d = $this->post;
            $w = new \app\common\model\Withdraw();
            $info = $this->db->find("withdraw",$d['id']);
            if (($info['shenhe_status']!=2 && $info['status']!=0)) {
                err('该订单状态错误');
            }
            if ($d['status']==1){
                $rs = $this->db->update('withdraw',['id'=>$d['id'],'daifa_status'=>3,'daifa_note'=>$d['note'],'daifa_time'=>time()]);
                m('Log','xcom')->record($this->userinfo["id"], '代发失败，订单id['.$d["id"].']','admin_user');
            }

            if ($d['status']==2){
                $rs = $this->db->update('withdraw',['id'=>$d['id'],'admin_user_id'=>$this->userinfo["id"],'status'=>2,'daifa_status'=>2,'shenhe_status_note'=>$d['note'],'update_time'=>time()]);
                m('Log','xcom')->record($this->userinfo["id"], '审核通过申请提现，订单id['.$d["id"].']','admin_user');

                $data = [
                    "table" => "withdraw",
                    "table_id" => $d['id'],
                    "title" => "提现到账通知",
                    "content" => '审核通过申请提现，订单id['.$d["id"].']',
                    "create_time" => time(),
                ];
                $this->db->insert("com_message",$data);
            }
            if ($rs) {
                session('js_msg','审核成功');
                cookie('js_function','preload');
                suc();
            }else{
                err('审核失败');
            }
        }
    }

    #代发
    public function startdf(){
        if (is_post()) {
            $d = $this->post;
            $rs = $this->db->find("withdraw",$d['id']);
            if ($rs['shenhe_status']!=2 || $rs['daifa_type']!='') {
                err('该订单状态错误');
            }
            $rs = $this->db->update('withdraw',['id'=>$rs['id'],'daifa_type'=>$d['daifa_type'],'daifa_time'=>time()]);
            if ($rs) {
                session('js_msg','操作成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('操作失败');
            }
        }
        $id = $this->get["id"];
        $rs = $this->db->find("withdraw",$id);
        $this->fetch(['info'=>$rs]);
    }

    //列表
    public function hand(){
      if(is_post()){
          $d = $this->post;
          $map = [
              ['and','shenhe_status','=',2],
              ['and','daifa_type','=',0],
          ];
          if($d['status']!='') $map[] = ['and','status','=',$d['status']];
          if($d['keyword']) $map[] = ['and',"user_name|order_no","like","%{$d['keyword']}%"];
          if($d['min_time']) $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
          if($d['max_time']) $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
          $w  = where($map);
          $sql = "select * from withdraw  $w[0] order by status asc,id desc".limit($d['page'],$d['limit']);
          $rs = $this->db->query($sql, $w[1],'',true);
          if($rs){
              foreach ($rs[0] as $k => $v) {
                  $rs[0][$k]["daifa_type_name"] = c("type.daifa_type.{$v['daifa_type']}");
                  $rs[0][$k]["status_name"] = c("type.withdraw_status.{$v['status']}");
                  $rs[0][$k]["shenhe_status_name"] = c("type.shenhe_status.{$v['shenhe_status']}");
                  $rs[0][$k]["daifa_status_name"] = c("type.daifa_status.{$v['daifa_status']}");
                  $rs[0][$k]["create_time"] = com_time_format($v['create_time']);
              }
              return suc($rs[0], $rs[1]);
          }else{
              suc([],0);
          }
      }
      $this->fetch();
    }

}
